package com.example.lncd.ui.auth;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.lncd.R;
import com.example.lncd.api.ApiService;
import com.example.lncd.api.RetrofitClient;
import com.example.lncd.model.User;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.textfield.TextInputEditText;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterFragment extends Fragment {

    private TextInputEditText etUsername, etPhone, etPassword, etConfirmPassword;
    private Button btnRegister;
    private ApiService apiService;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register, container, false);
        
        // 初始化视图
        etUsername = view.findViewById(R.id.etUsername);
        etPhone = view.findViewById(R.id.etPhone);
        etPassword = view.findViewById(R.id.etPassword);
        etConfirmPassword = view.findViewById(R.id.etConfirmPassword);
        btnRegister = view.findViewById(R.id.btnRegister);
        
        // 初始化API服务
        apiService = RetrofitClient.getApiService();
        
        // 设置注册按钮点击事件
        btnRegister.setOnClickListener(v -> registerUser());
        
        return view;
    }
    
    private void registerUser() {
        String username = etUsername.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();
        
        // 验证输入
        if (username.isEmpty() || phone.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
            Toast.makeText(requireContext(), "请填写所有字段", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (!password.equals(confirmPassword)) {
            Toast.makeText(requireContext(), "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (phone.length() != 11) {
            Toast.makeText(requireContext(), "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 创建注册用户对象
        User registerUser = new User(username, phone, password);
        
        // 调用注册API
        Call<User> call = apiService.registerUser(registerUser);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful() && response.body() != null) {
                    Toast.makeText(requireContext(), "注册成功，请登录", Toast.LENGTH_SHORT).show();
                    
                    // 切换到登录tab
                    TabLayout tabLayout = requireActivity().findViewById(R.id.tabLayout);
                    tabLayout.getTabAt(0).select();
                    
                    // 清空输入框
                    clearInputs();
                } else {
                    Toast.makeText(requireContext(), "注册失败：用户名或手机号已存在", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(requireContext(), "网络错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    private void clearInputs() {
        etUsername.setText("");
        etPhone.setText("");
        etPassword.setText("");
        etConfirmPassword.setText("");
    }
} 